import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  let env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]'
      }),
      viteMockServe({
        enable: true
      }),
      VueSetupExtend()
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/variable.scss";`,
          javascriptEnabled: true
        }
      }
    },
    server: {
      proxy: {
        [env.VITE_APP_BASE_USER_API]: {
          target: env.VITE_USER_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp('^\\' + env.VITE_APP_BASE_USER_API), '')
        },
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp('^\\' + env.VITE_APP_BASE_API), '')
        },
        // '/newApi': {
        //   target: 'http://39.98.123.211:8510/',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/newApi/, ''),
        // }
      }
    }
  }
})
